package yuejian_07.yuejian_07.model;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import yuejian_07.yuejian_07.RetrofitListener;
import yuejian_07.yuejian_07.activity.MasInfoActivity;
import yuejian_07.yuejian_07.activity.MasSearchActivity;
import yuejian_07.yuejian_07.fragment.MasterClassFragment;
import yuejian_07.yuejian_07.fragment.MasterQuesFragment;
import yuejian_07.yuejian_07.javabean.Master;
import yuejian_07.yuejian_07.javabean.Question;
import yuejian_07.yuejian_07.javabean.Train;
import yuejian_07.yuejian_07.service.MasterService;

import static yuejian_07.yuejian_07.base.Constant.BASE_URL;

/**
 * Created by dell on 2017/10/25.
 */

public class MasterModel {
    private Retrofit retrofit;
    public MasterModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /*******首页中权威大师列表******/
    public void masterlist( final RetrofitListener listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getAllMasters();
        Callback<List<Master>> callback=new Callback<List<Master>>(){
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response ){
                List<Master> data=response.body();
                listener.onSuccess(data);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Master>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }


    //获取大师详情
    public void getMasterById(int Master_id, final MasInfoActivity listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterById(Master_id);
        Callback<Master> callback=new Callback<Master>(){
            @Override
            public void onResponse(Call<Master> call, Response<Master> response){

                listener.onSuccess(response.body());
            }
            @Override
            public void onFailure(Call<Master> call,Throwable t){
                t.printStackTrace();
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    /*******大师详情中大师课堂列表，引用Train(培训)相关数据******/
    public void classlist(int Master_id, final MasterClassFragment listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterTrains(Master_id);
        Callback<List<Train>> callback=new Callback<List<Train>>(){
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response ){
                List<Train> data=response.body();
                listener.onSuccess(data);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Train>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    /*******大师详情中大师问答列表，引用Question(问答)相关数据******/
    public void Qalist(int Master_id, final MasterQuesFragment listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterQA(Master_id);
        Callback<List<Question>> callback=new Callback<List<Question>>(){
            @Override
            public void onResponse(Call<List<Question>> call, Response<List<Question>> response ){
                List<Question> data=response.body();
                listener.onSuccess(data);//userId这里有问题
            }
            @Override
            public  void onFailure(Call<List<Question>> call,Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }
    public void getMasBySearch(String SearchKey, final MasSearchActivity listener){
        MasterService masterService=retrofit.create(MasterService.class);
        Call call=masterService.getMasterBySearch(SearchKey);
        Callback<List<Master>> callback=new Callback<List<Master>>(){
            @Override
            public void onResponse(Call<List<Master>> call, Response<List<Master>> response ){
                List<Master> data=response.body();
                listener.onSuccess(data);
            }
            @Override
            public  void onFailure(Call<List<Master>> call, Throwable t){
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

}